package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableUser;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/9/4
 */
public class UserController {

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行分页查询
        List<BootStrapTableUser> list = UserService.findAll(true, offset, pageNumber);
        //4.获取数据总条数
        Map<String, Integer> console = UserService.console();
        Integer total = console.get("data_size");
        //5.将集合封装为bootstrap-table识别的格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String uname = request.getParameter("uname");
        String uphonenumber = request.getParameter("uphonenumber");
        String uidcardnum = request.getParameter("uidcardnum");
        String upassword = request.getParameter("upassword");
        User user = new User(uname,uphonenumber,uidcardnum,upassword);
        boolean flag = UserService.insert(user);
        Message msg = new Message();
        if (flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("用户信息录入成功!");
        }else {
            //录入失败
            msg.setStatus(-1);
            msg.setResult("用户信息录入失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String phoneNumber = request.getParameter("phoneNumber");
        User user = UserService.findByPhoneNumber(phoneNumber);
        Message msg = new Message();
        if (user == null){
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String uname = request.getParameter("uname");
        String uPhoneNumber = request.getParameter("uPhoneNumber");
        String uIdCardNum = request.getParameter("uIdCardNum");
        String uPassword = request.getParameter("uPassword");
        User user = UserService.findById(id);
        user.setUname(uname);
        user.setuPhoneNumber(uPhoneNumber);
        user.setuIdCardNum(uIdCardNum);
        user.setuPassword(uPassword);
        boolean flag = UserService.update(id, user);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = UserService.delete(id);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
